# -*- coding: cp1252 -*-
#   Autor: Joao Siqueira
#   Proposito: Gerar grids para a area de um shapefile.
#
import sys, string, os, arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.overwriteOutput = 1

# Input workspace
gp.workspace = "C:/Users/joaovictor/Dropbox/Programming/Python/test/shapes"

# Output workspace
outputDir = "C:/Users/joaovictor/Dropbox/Programming/Python/test/output"

# Define grid cell size (in meters)
xCellSize = 2000
yCellSize = 2000

# Create a list of input shapefiles
fcList = gp.ListFeatureClasses()

print fcList

for fClass in fcList:
    try:
        ext = gp.describe(fClass).extent

        xMin  = str(ext.xmin)
        xMax  = str(ext.xmax)
        yMin  = str(ext.ymin)
        yMax  = str(ext.ymax)
        yAxis = str(ext.ymin + 10)
        
        print "xMin = ", xMin, "xMax = ", xMax
        print "yMin = ", yMin, "yMax = ", yMax
        
        # Run the geoprocessor
        gp.CreateFishnet_management(outputDir + '/' + fClass[0:len(fClass)-4] + '_grid_' + str(xCellSize) + '.shp', (xMin + " " + yMin), (xMin + " " + yAxis), xCellSize, yCellSize, round(1+(ext.ymax - ext.ymin)/yCellSize), round(1+(ext.xmax - ext.xmin)/xCellSize),  "#", "NO_LABELS", fClass)
    except:
        print "Problemas com: ", fClass
print ":)"


